package wqh.controller.json;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import wqh.domain.GeoLocation;
import wqh.domain.WaterChannel;
import wqh.domain.WaterDevice;
import wqh.global.GlobalWaterAlarm;
import wqh.global.GlobalWaterChannel;
import wqh.pojo.MapRet;
import wqh.service.GeoLocationService;
import wqh.service.WaterChannelService;
import wqh.service.WaterDeviceService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by wqh on 2017/1/13.
 */
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/json/water_device/channel")
public class WaterChannelJSON {

    @Autowired
    private WaterChannelService waterChannelService;

    @Autowired
    private GeoLocationService geoLocationService;

    @Autowired
    private WaterDeviceService waterDeviceService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Object add(@RequestBody WaterChannel c){
        try {
            if(c.getChannel_number() < 1 || c.getChannel_number() > 4) return MapRet.SQL_INSERT_ERROR();
            waterChannelService.save(c);
            WaterChannel w = waterChannelService.findByChannelAndDeviceId(c.getChannel_number(),c.getWaterDevice().getId());
            if(w != null){
                GlobalWaterChannel.addDevice(w.getWaterDevice().getIp() + ":" + w.getWaterDevice().getPort() + "H" + w.getChannel_number(),w.getHigh());
                GlobalWaterChannel.addDevice(w.getWaterDevice().getIp() + ":" + w.getWaterDevice().getPort() + "L" + w.getChannel_number(),w.getLow());
            }
            return MapRet.SQL_INSERT_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_INSERT_ERROR();
        }
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@RequestBody WaterChannel c){
        try {
            System.out.println(c);
            WaterChannel w = waterChannelService.findById(c.getId());
            if(w == null) return MapRet.SQL_UPDATE_ERROR();
            if(c.getChannel_number() != null) w.setChannel_number(c.getChannel_number());
            if(c.getHigh() != null) w.setHigh(c.getHigh());
            if(c.getLow() != null) w.setLow(c.getLow());
            if(c.getName() != null) w.setName(c.getName());
            if(c.getPos_x() != null) w.setPos_x(c.getPos_x());
            if(c.getPos_y() != null) w.setPos_y(c.getPos_y());
            w.setType(c.isType());
            if(c.getGeoLocation() != null && c.getGeoLocation().getId() != null) w.getGeoLocation().setId(c.getGeoLocation().getId());
            if(c.getWaterDevice() !=null && c.getWaterDevice().getId() != null) {
                w.getGeoLocation().setId(c.getWaterDevice().getId());
                GlobalWaterChannel.addDevice(w.getWaterDevice().getIp() + ":" + w.getWaterDevice().getPort() + "H" + w.getChannel_number(),w.getHigh());
                GlobalWaterChannel.addDevice(w.getWaterDevice().getIp() + ":" + w.getWaterDevice().getPort() + "L" + w.getChannel_number(),w.getLow());
            }
            waterChannelService.update(w);
            return MapRet.SQL_UPDATE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_UPDATE_ERROR();
        }
    }


    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Object delete(@RequestBody WaterChannel c) {
        try {
            waterChannelService.delete(c);
            return MapRet.SQL_DELETE_SUCCESS();
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.SQL_DELETE_ERROR();
        }
    }

    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public Object getAll(){
        List<WaterChannel> list = waterChannelService.findAll();
        List<GeoLocation> gList = geoLocationService.findAll();
        if(list == null || gList == null) return MapRet.DATA(null);
        List<Map<String,Object>> listRet = new ArrayList<>();
        for(WaterChannel w : list){
            String geo1 = "无";
            String geo2 = "无";
            String geo3 = "无";
            if(w.getGeoLocation().getLevel() == 3){
                geo3 = w.getGeoLocation().getName();
                GeoLocation g2 = geoLocationService.findById(w.getGeoLocation().getParent_id());
                if(g2 != null){
                    geo2 = g2.getName();
                    GeoLocation g1 = geoLocationService.findById(w.getGeoLocation().getParent_id());
                    if(g1 != null){
                        geo1 = g1.getName();
                    }
                }
            }
            Map<String,Object> map = new HashMap<>();
            map.put("id",w.getId());
            map.put("geo1",geo1);
            map.put("geo2",geo2);
            map.put("geo3",geo3);
            map.put("device_id",w.getWaterDevice().getDevice_id());
            map.put("channel_number",w.getChannel_number());
            map.put("name",w.getName());
            map.put("pos_x",w.getPos_x());
            map.put("pos_y",w.getPos_y());
            map.put("type",w.isType());
            map.put("geo_3_id",w.getGeoLocation().getId());
            listRet.add(map);
        }
        return MapRet.DATA(listRet);
    }

    @RequestMapping(value = "/getAllStatus",method = RequestMethod.POST)
    public Object getAllStatus(@RequestBody Map<String,Long> map){
        try {
            System.out.print("********************");
            System.out.println(map);
            GeoLocation g = geoLocationService.findById(map.get("geo_id"));
            if(g == null) return MapRet.DATA(null);
            List<Map<String,Object>> mapList = new ArrayList<>();
            if(g.getLevel() == 1){
                List<GeoLocation> g2List = geoLocationService.findByParentID(g.getId());
                if(g2List == null) return MapRet.DATA(null);
                for(GeoLocation g2 : g2List){
                    List<GeoLocation> g3List = geoLocationService.findByParentID(g2.getId());
                    Map<String, Object> data = new HashMap<>();
                    data.put("geo2_id",g2.getId());
                    data.put("status",false);
                    data.put("name",g2.getName());
                    data.put("pos_x",g2.getPos_x());
                    data.put("pos_y",g2.getPos_y());
                    if(g3List.size() != 0){
                        for(GeoLocation g3 : g3List){
                            List<WaterChannel> list = waterChannelService.findByGeoId(g3.getId());
                            if(list != null ){
                                for(WaterChannel w : list){
                                    boolean isAlarm = GlobalWaterAlarm.getStatus(w.getWaterDevice().getDevice_id() +"@"+w.getChannel_number()) ||
                                            GlobalWaterAlarm.getStatus(w.getWaterDevice().getDevice_id() +"#"+w.getChannel_number());
                                    if(isAlarm){
                                        data.put("status",true);
                                        break;
                                    }
                                }
                            }
                        }
                    }
                    mapList.add(data);
                }
            }

            if(g.getLevel() == 2){
                List<GeoLocation> g3List = geoLocationService.findByParentID(g.getId());
                if(g3List == null) return MapRet.DATA(null);
                for(GeoLocation g3 : g3List){
                    Map<String, Object> data = new HashMap<>();
                    data.put("geo3_id",g3.getId());
                    data.put("status",false);
                    data.put("name",g3.getName());
                    data.put("pos_x",g3.getPos_x());
                    data.put("pos_y",g3.getPos_y());
                    List<WaterChannel> list = waterChannelService.findByGeoId(g3.getId());
                    if(list != null ){
                        for(WaterChannel w : list){
                            boolean isAlarm = GlobalWaterAlarm.getStatus(w.getWaterDevice().getDevice_id() +"@"+w.getChannel_number()) ||
                                    GlobalWaterAlarm.getStatus(w.getWaterDevice().getDevice_id() +"#"+w.getChannel_number());
                            if(isAlarm){
                                data.put("status",true);
                                break;
                            }
                        }
                    }
                    mapList.add(data);
                }
            }

            if(g.getLevel() == 3){
                List<WaterChannel> list = waterChannelService.findByGeoId(map.get("geo_id"));
                if(list != null) {
                    for(WaterChannel x : list){
                        Map<String, Object> data = new HashMap<>();
                        data.put("monitor_id",x.getId());
                        data.put("monitor_name",x.getName());
                        data.put("pos_x",x.getPos_x());
                        data.put("pos_y",x.getPos_y());
                        data.put("status",GlobalWaterAlarm.getStatus(x.getWaterDevice().getDevice_id() +"@"+x.getChannel_number()) ||
                                GlobalWaterAlarm.getStatus(x.getWaterDevice().getDevice_id() +"#"+x.getChannel_number()));
                        mapList.add(data);
                    }
                }
            }

            return MapRet.DATA(mapList);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

    @RequestMapping(value = "/getGeoInfo",method = RequestMethod.POST)
    public Object getGeoInfo(@RequestBody Map<String,Object> map){
        try {
            long device_id = Long.valueOf(map.get("device_id").toString());
            int channel_number = Integer.valueOf(map.get("channel_number").toString());
            WaterDevice device = waterDeviceService.findByDeviceId(device_id);
            if(device == null) return MapRet.DATA(null);
            WaterChannel x = waterChannelService.findByChannelAndDeviceId(channel_number,device.getId());
            if(x == null) return MapRet.DATA(null);
            String geo3 = x.getGeoLocation().getName();
            String geo2 = "无";
            String geo1 = "无";
            GeoLocation g2 = geoLocationService.findById(x.getGeoLocation().getParent_id());
            if(g2 != null){
                geo2 = g2.getName();
                GeoLocation g1 = geoLocationService.findById(g2.getParent_id());
                if(g1 != null){
                    geo1 = g1.getName();
                }
            }
            Map<String,Object> data = new HashMap<>();
            data.put("geo_1_name",geo1);
            data.put("geo_2_name",geo2);
            data.put("geo_3_name",geo3);
            data.put("geo_id",x.getGeoLocation().getId());

            return MapRet.DATA(data);
        }catch (Exception e){
            e.printStackTrace();
            return MapRet.DATA(null);
        }
    }

}
